import 'package:flutter/material.dart';
import 'package:english_words/english_words.dart';
import 'package:myapp/canvas.dart';
import 'package:myapp/dartTest.dart';

import 'add_remove_view.dart';
import 'animateTest.dart';
import 'counterWidget.dart';
import 'cupertinoTest.dart';
import 'customButtom.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: RandomWords(),
      theme: new ThemeData(
        primaryColor:Colors.red
      ),
    );
  }
}

class RandomWords extends StatefulWidget {
  @override
  State createState() => ButtonListState();
}

class ButtonListState extends State{
  List<String> nameList=<String>["移除和添加view","动画测试","Canvas测试",
    "自定义view","DART语言测试","生命周期","IOS风格控件"];
  List<Widget> widgetList=<Widget>[AddAndRemoveViewTest(),FadeAppTest(),DemoApp(),
    CustomViewTest(),DartTest(),CounterTestWidget(),CupertinoTestRoute()];

  @override
  Widget build(BuildContext context) {
    // TODO: implement build

    return Scaffold(
      appBar:AppBar(
        title:Text('功能列表'),
      ),
      body:_buildList(),
    );
  }
  _buildList(){
    return new ListView.builder(itemBuilder: (context,i){
          if (i>=nameList.length){
            return null;
          }
          return ListTile(
            title:new Text(nameList[i]),
            trailing: Icon(Icons.keyboard_arrow_right),
            onTap: (){
              Navigator.of(context).push(
                new MaterialPageRoute(builder: (context){
                  return widgetList[i];
                })
              );
            },
          );
    });
  }
}

class RandomWordsState extends State<RandomWords> {
  final _suggestions = <WordPair>[];
  final _saved=new Set<WordPair>();
  final _biggerFont = const TextStyle(fontSize: 18.0);
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Startup Name Generator'),
        actions: <Widget>[
          new IconButton(icon: const Icon(Icons.list), onPressed: _pushSaved)
        ],
      ),
      body: _buildSuggestions(),
    );
  }

  Widget _buildSuggestions() {
    return ListView.builder(
        padding: const EdgeInsets.all(16.0),
        itemBuilder: /*1*/ (context, i) {
          if (i.isOdd) return Divider(); /*2*/

          final index = i ~/ 2; /*3*/
          if (index >= _suggestions.length) {
            _suggestions.addAll(generateWordPairs().take(10)); /*4*/
          }
          return _buildRow(_suggestions[index]);
        });
  }

  Widget _buildRow(WordPair pair) {
    final alreadySaved=_saved.contains(pair);
    return ListTile(
      title: Text(
        pair.asPascalCase,
        style: _biggerFont,
      ),
      trailing: new Icon(
        alreadySaved?Icons.favorite:Icons.favorite_border,
        color:alreadySaved?Colors.red:null
      ),
      onTap: (){
        setState(() {
          if(alreadySaved){
            _saved.remove(pair);
          }else{
            _saved.add(pair);
          }
        });
      },
    );
  }

void _pushSaved() {
  Navigator.of(context).push(
    new MaterialPageRoute(builder: (context){
      final tiles=_saved.map((pair){
        return new ListTile(
          title:new Text(
            pair.asPascalCase,
            style:_biggerFont,
          )
        );
      });
      final divided=ListTile.divideTiles(tiles: tiles,context:context).toList();
      return new Scaffold(
        appBar: new AppBar(
          title:new Text('Saved Suggestions'),
        ),
        body:new ListView(children:divided)
      );
    })
  );
}
}
